﻿Imports PBE.Market.Logic.Contracts
Imports System.Data

Namespace Data

    Public Class cDataResulrWriter

        Property ErrorMessage As String
        Property Result As Boolean

        Dim DBContext As New MaturityLadderEntities()



        Function SaveDatatoDB(ByVal Contract As iContract) As Boolean
            Dim DBContract As New tblContract
            Try
                With DBContract
                    .ContractMaturityDate = Contract.MaturityDate
                    .ContractBalance = Contract.Balance
                    .ContractInterestRate = Contract.InterestRate
                    .ContractType = Contract.ContractType
                    .ContractNumber = Contract.ContractID
                    .ContractCurrency = Contract.Currency
                    .ContractCycleOfPayment = Contract.CycleOfPayment
                    .ContractMajorGroup = Contract.ContractMajorGroup
                    .ContractProductCode = Contract.ContractProductCode
                    .ContractProductDiscription = Contract.ContractProductDiscription
                    .ContractTag = Contract.ContractTag
                    .ContractResponsableGL = Contract.ResponsableGL
                End With

                For Each Pmt As PaymentSchedual In Contract.PaymentSchedaul
                    Dim DBpayment As New tblPayment
                    With DBpayment
                        .PaymentOrder = Pmt.intPaymentOrder
                        .PaymnetAmount = Pmt.dblPaymentAmount
                        .PaymentPrincipalAmount = Pmt.dblPrincipalPayment
                        .PaymentInterestAmount = Pmt.dblInterestPayment
                        .PaymentDays = Pmt.intPaymentDays
                        .PaymentDate = Pmt.dtePaymentDate
                    End With
                    DBContract.tblPayments.Add(DBpayment)
                Next

                DBContext.AddTotblContracts(DBContract)
                'DBContext.SaveChanges()

                Me.Result = True
            Catch ex As Exception
                Me.ErrorMessage = ex.Message
                Me.Result = False
            End Try

            DBContract = Nothing

            Return Result
        End Function

        Function SaveData() As Boolean
            Try
                DBContext.SaveChanges(Objects.SaveOptions.None)
                Return True
            Catch ex As Exception
                ErrorMessage = ex.Message
                Return False
            End Try

        End Function

        Sub New()
            MyBase.New()
        End Sub


        Sub Dispose()
            DBContext.Dispose()
        End Sub
    End Class
End Namespace